package com.sky.controller.client;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.domain.Blog;
import com.sky.domain.Tag;
import com.sky.service.BlogService;
import com.sky.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author 尹稳健~
 * @version 1.0
 */
@Controller
public class TagShowController {

    @Autowired
    private TagService tagService;

    @Autowired
    private BlogService blogService;

    @GetMapping("/tags/{id}")
    public String types(@PathVariable("id") Long id, Model model,
                        @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum){
        PageHelper.startPage(pageNum,10);
        List<Tag> tags = tagService.getBlogTag();
        //-1从导航点过来的
        if (tags != null && tags.size() > 0) {
            if (id == -1) {
                id = tags.get(0).getId();
            }
        }
        List<Blog> blogList = blogService.getByTagId(id);
        PageInfo<Blog> pageInfo = new PageInfo<>(blogList);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("tags",tags);
        model.addAttribute("activeTagId", id);
        return "tags";
    }
}
